var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/functionalComponent/table/hook/useKeyDown.ts
var useKeyDown_exports = {};
__export(useKeyDown_exports, {
  useKeyDown: () => useKeyDown
});
module.exports = __toCommonJS(useKeyDown_exports);
var import_react = require("react");
var import_util = require("../util");
function useKeyDown(elRef, deps) {
  const [table, editableColumnIndex, maxRowIndex, editable] = deps;
  const { allColumns } = table.generateColumns();
  const listenerOnDocument = table.props.listenerOnDocument;
  const cb = (0, import_react.useMemo)(() => {
    if (!editable) {
      return import_util.emptyFn;
    }
    const startEditing = ({ rowIndex, columnIndex }, { keyCode }) => {
      return table.startEditing(
        {
          rowIndex,
          dataIndex: allColumns[columnIndex].dataIndex
        },
        { keyCode }
      );
    };
    const getNextEditColumnIndex = (columnIndex) => {
      return editableColumnIndex.find((c) => c > columnIndex) ?? columnIndex;
    };
    const getPrevEditColumnIndex = (columnIndex) => {
      const arr = editableColumnIndex.filter((c) => c < columnIndex);
      return arr.length > 0 ? arr[arr.length - 1] : Math.max(columnIndex, 0);
    };
    const minColumnIndex = editableColumnIndex[0] || 0;
    const maxColumnIndex = editableColumnIndex[editableColumnIndex.length - 1];
    const addRow = (rowIndex, columnIndex, { keyCode }) => {
      const autoAddRow = table.props.keyBoardToAddRow;
      if (autoAddRow && rowIndex >= maxRowIndex) {
        table.addRows().then(() => {
          startEditing(
            {
              rowIndex: rowIndex + 1,
              columnIndex
            },
            { keyCode }
          );
        });
        return true;
      }
      return false;
    };
    return (e) => {
      var _a, _b, _c, _d, _e, _f;
      const {
        selected: { rowIndex, dataIndex, editing }
      } = table.state;
      const el = document.activeElement || {};
      const disabled = table.props.disabled;
      if (disabled === false)
        return;
      if (el.tagName !== "BODY" && !import_util.util.closest(e.target, (dom) => dom === elRef.current)) {
        return;
      }
      const columnIndex = allColumns.findIndex((c) => c.dataIndex === dataIndex);
      const { shiftKey, ctrlKey } = e;
      if (shiftKey && e.keyCode === 16) {
        return;
      }
      if (editing && (el.tagName === "INPUT" || el.tagName === "TEXTAREA")) {
        if (shiftKey && el.tagName === "TEXTAREA" && e.keyCode === 13) {
          return;
        }
        const start = import_util.util.getCursorPosition();
        const end = import_util.util.getSelectionEnd();
        if ([37, 39].indexOf(e.keyCode) > -1) {
          if (start !== end) {
            import_util.util.setCursorPosition(el, 37 === e.keyCode ? start : end);
            return;
          }
          if (start > 0 && 37 === e.keyCode || start > -1 && start < ((_a = el.value) == null ? void 0 : _a.length) && 39 === e.keyCode) {
            return;
          }
        }
      }
      if (((_c = (_b = table.props).onBeforeEditCellKeyDown) == null ? void 0 : _c.call(_b, e, { rowIndex, columnIndex })) === false) {
        return;
      }
      let retValue = false;
      switch (e.keyCode) {
        case 9:
        case 13:
          if (shiftKey && e.keyCode === 9) {
            if (rowIndex === -1 || columnIndex === -1) {
              retValue = startEditing(
                {
                  rowIndex: maxRowIndex,
                  columnIndex: maxColumnIndex
                },
                e
              );
            } else if (columnIndex === minColumnIndex) {
              retValue = rowIndex > 0 ? startEditing(
                {
                  rowIndex: rowIndex - 1,
                  columnIndex: maxColumnIndex
                },
                e
              ) : true;
            } else {
              retValue = startEditing(
                {
                  rowIndex,
                  columnIndex: getPrevEditColumnIndex(columnIndex)
                },
                e
              );
            }
          } else {
            if (rowIndex === -1 || columnIndex === -1) {
              retValue = startEditing(
                {
                  rowIndex: 0,
                  columnIndex: minColumnIndex
                },
                e
              );
            } else if (columnIndex === maxColumnIndex) {
              if (rowIndex < maxRowIndex) {
                retValue = startEditing(
                  {
                    rowIndex: rowIndex + 1,
                    columnIndex: minColumnIndex
                  },
                  e
                );
              } else {
                retValue = addRow(rowIndex, minColumnIndex, e);
              }
            } else {
              retValue = startEditing(
                {
                  rowIndex,
                  columnIndex: getNextEditColumnIndex(columnIndex)
                },
                e
              );
            }
          }
          break;
        case 35:
          retValue = startEditing(
            {
              rowIndex: maxRowIndex,
              columnIndex: Math.max(minColumnIndex, columnIndex)
            },
            e
          );
          break;
        case 36:
          retValue = startEditing(
            {
              rowIndex: 0,
              columnIndex: Math.max(minColumnIndex, columnIndex)
            },
            e
          );
          break;
        case 37:
          retValue = startEditing(
            {
              rowIndex: Math.max(0, rowIndex),
              columnIndex: getPrevEditColumnIndex(columnIndex)
            },
            e
          );
          break;
        case 38:
          retValue = startEditing(
            {
              rowIndex: Math.max(0, rowIndex - 1),
              columnIndex: Math.max(minColumnIndex, columnIndex)
            },
            e
          );
          break;
        case 39:
          retValue = startEditing(
            {
              rowIndex: Math.max(0, rowIndex),
              columnIndex: getNextEditColumnIndex(columnIndex)
            },
            e
          );
          break;
        case 40:
          if (rowIndex < maxRowIndex) {
            retValue = startEditing(
              {
                rowIndex: rowIndex + 1,
                columnIndex: Math.max(minColumnIndex, columnIndex)
              },
              e
            );
          } else {
            retValue = addRow(rowIndex, Math.max(minColumnIndex, columnIndex), e);
          }
          break;
        default:
          break;
      }
      if (retValue) {
        (_d = elRef.current) == null ? void 0 : _d.focus();
        e.preventDefault();
        e.stopPropagation();
      } else {
        table.notify(e.keyCode, "keyboardEvent").then();
      }
      (_f = (_e = table.props).onAfterEditCellKeyDown) == null ? void 0 : _f.call(_e, e, { rowIndex, columnIndex });
    };
  }, deps);
  (0, import_react.useEffect)(() => {
    if (listenerOnDocument && editable) {
      document.addEventListener("keydown", cb);
      return () => {
        document.removeEventListener("keydown", cb);
      };
    }
  }, [cb, listenerOnDocument, editable]);
  return listenerOnDocument || !editable ? null : cb;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useKeyDown
});
